package reviewAlgorithms.greedyThought;

public class MaxProfit {

    public static void main(String[] args) {
        int[] prices = {7,1,5,3,6,4};
        System.out.println(maxProfit(prices));
    }

    public static int maxProfit(int[] prices) {
        if (prices == null || prices.length < 1){
            return 0;
        }
        int max = 0;
        int soFarMin = prices[0];
        for (int i = 0; i < prices.length; i++) {
            if (soFarMin > prices[i]){
                soFarMin = prices[i];
            }else {
                max = Math.max(max,prices[i] - soFarMin);
            }
        }
        return max;
    }

}
